package com.zxjbyte.yiyi.module.system.service.dict.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.cglib.CglibUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zxjbyte.yiyi.framework.common.domain.api.PageResult;
import com.zxjbyte.yiyi.framework.common.exception.ServiceException;
import com.zxjbyte.yiyi.framework.common.util.CollXUtil;
import com.zxjbyte.yiyi.framework.mybatis.service.impl.BaseServiceXImpl;
import com.zxjbyte.yiyi.framework.mybatis.util.Condition;
import com.zxjbyte.yiyi.module.system.domain.entity.DictDataPO;
import com.zxjbyte.yiyi.module.system.domain.request.dict.DictDataPageRequest;
import com.zxjbyte.yiyi.module.system.domain.response.dict.DictDataDetailResponse;
import com.zxjbyte.yiyi.module.system.domain.response.dict.DictDataPageResponse;
import com.zxjbyte.yiyi.module.system.mapper.dict.DictDataMapper;
import com.zxjbyte.yiyi.module.system.service.dict.IDictDataService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

import static com.zxjbyte.yiyi.framework.common.enums.SortOrder.DESC;

/**
 * @Author zhangxingjia
 * @Date 2024/1/24 16:18
 * @Version: 1.0
 */
@Service
public class DictDataServiceImpl extends BaseServiceXImpl<DictDataMapper, DictDataPO> implements IDictDataService {

    @Override
    public PageResult<DictDataPageResponse> page(DictDataPageRequest request) {
        LambdaQueryWrapper<DictDataPO> wrapper = Wrappers.<DictDataPO>lambdaQuery().eq(StrUtil.isNotBlank(request.getDictType()), DictDataPO::getDictType, request.getDictType())
                .eq(StrUtil.isNotBlank(request.getDictLabel()), DictDataPO::getDictLabel, request.getDictLabel())
                .eq(Objects.nonNull(request.getStatus()), DictDataPO::getStatus, request.getStatus())
                .ge(Objects.nonNull(request.getStartTime()), DictDataPO::getCreateTime, request.getStartTime())
                .le(Objects.nonNull(request.getEndTime()), DictDataPO::getCreateTime, request.getEndTime());
        IPage<DictDataPO> page = page(Condition.getPage(request, DictDataPO::getCreateTime, DESC), wrapper);
        List<DictDataPO> records = page.getRecords();
        List<DictDataPageResponse> responseList = null;
        if(CollXUtil.isNotEmpty(records)){
            responseList = CglibUtil.copyList(records, DictDataPageResponse::new);
        }
        return PageResult.of(responseList, page);
    }

    @Override
    public DictDataDetailResponse detail(Long id) {
        DictDataPO entity = getById(id);
        if(Objects.isNull(entity)){
            throw new ServiceException("字典数据不存在");
        }
        return CglibUtil.copy(entity, DictDataDetailResponse.class);
    }

    @Override
    public List<DictDataPO> getByDictType(String dictType) {
        return lambdaQuery().eq(DictDataPO::getDictType, dictType).list();
    }

}
